package com.cys.markdown.doc.project.service;

import com.cys.common.util.base.IdWorker;
import com.cys.common.util.base.StringUtil;
import com.cys.markdown.doc.project.interfaces.dao.ProjectInfoDao;
import com.cys.markdown.doc.project.interfaces.entity.IProjectInfo;
import com.cys.markdown.doc.project.entity.ProjectInfo;
import com.cys.markdown.doc.project.interfaces.service.ProjectInfoService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.cys.common.util.base.MapUtil;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import javax.annotation.Resource;

/**
 * T_PROJECT_INFO【项目信息表】 Service接口实现类
 *
 * @author caiyushen
 */
@Service("projectInfoServiceImpl")
public class ProjectInfoServiceImpl implements ProjectInfoService {

    private static Logger logger = LoggerFactory.getLogger(ProjectInfoServiceImpl.class);

    @Autowired
    private ProjectInfoDao projectInfoDao;


    /**
     * 新增ProjectInfo
     *
     * @param params ProjectInfo实体类对应参数
     */
    @Override
    public IProjectInfo save(Map<String, Object> params) throws Exception {
        if (!StringUtil.checkObj(params)) {
            logger.error("method[save] params is null.");
            return null;
        }
        IProjectInfo projectInfo = (IProjectInfo) MapUtil.toObject(ProjectInfo.class, params);
        projectInfo.setProjectInfoId(IdWorker.getId());
        projectInfoDao.save(projectInfo);
        return projectInfo;
    }

    /**
     * 修改ProjectInfo
     *
     * @param params ProjectInfo实体类对应参数
     */
    @Override
    public IProjectInfo update(Map<String, Object> params) throws Exception {
        if (!StringUtil.checkObj(params)) {
            logger.error("method[update] params is null.");
            return null;
        }
        Long id = MapUtil.getLong(params, "projectInfoId");
        if (!StringUtil.checkObj(id)) {
            logger.error("method[update] params.id is null.");
            return null;
        }
        IProjectInfo projectInfo = (IProjectInfo) MapUtil.toObject(ProjectInfo.class, params);
        projectInfoDao.update(projectInfo);
        return projectInfo;
    }

    /**
     * 修改ProjectInfo
     *
     * @param projectInfo ProjectInfo实体类
     * @return ProjectInfo实体类
     */
    @Override
    public IProjectInfo update(IProjectInfo projectInfo) {
        if (!StringUtil.checkObj(projectInfo)) {
            logger.error("method[update] projectInfo is null.");
            return null;
        }
        if (!StringUtil.checkObj(projectInfo.getProjectInfoId())) {
            logger.error("method[update] params.id is null.");
            return null;
        }
        projectInfoDao.update(projectInfo);
        return projectInfo;
    }

    /**
     * 通过id物理删除ProjectInfo
     *
     * @param id ProjectInfo主键
     */
    @Override
    public void deleteById(Long id) {
        if (!StringUtil.checkObj(id)) {
            logger.warn("method[deleteById] id is null.");
            return;
        }
        projectInfoDao.deleteById(id);
    }

    /**
     * 批量物理删除ProjectInfo
     *
     * @param ids ProjectInfo主键数组
     */
    @Override
    public void deleteBatch(Long[] ids) {
        if (!StringUtil.checkObj(ids)) {
            logger.warn("method[deleteBatch] ids is null.");
            return;
        }
        projectInfoDao.deleteBatch(ids);
    }

    /**
     * 通过id逻辑删除ProjectInfo
     *
     * @param id ProjectInfo主键
     */
    @Override
    public void logicDeleteById(Long id) {
        if (!StringUtil.checkObj(id)) {
            logger.warn("method[logicDeleteById] id is null.");
            return;
        }
        projectInfoDao.logicDeleteById(id);
    }

    /**
     * 批量逻辑删除ProjectInfo
     *
     * @param ids ProjectInfo主键数组
     */
    @Override
    public void logicDeleteBatch(Long[] ids) {
        if (!StringUtil.checkObj(ids)) {
            logger.warn("method[logicDeleteBatch] ids is null.");
            return;
        }
        projectInfoDao.logicDeleteBatch(ids);
    }


    /**
     * 根据id加载ProjectInfo
     *
     * @param id ProjectInfo主键
     */
    @Override
    public IProjectInfo find(Long id) {
        if (!StringUtil.checkObj(id)) {
            logger.warn("method[find] id is null.");
            return null;
        }
        return projectInfoDao.findEntityById(id);
    }

    /**
     * 获取ProjectInfo列表数据
     *
     * @param map 默认空数据集
     */
    @Override
    public List<IProjectInfo> findList(Map<String, Object> map) {
        return (List<IProjectInfo>) projectInfoDao.findList(map);
    }

    /**
     * 获取一级项目列表
     *
     * @param param-projectName 项目名称
     */
    @Override
    public List<IProjectInfo> findParentList(Map param) {
        return (List<IProjectInfo>) projectInfoDao.findParentList(param);
    }

    /**
     * 分页获取数据
     *
     * @param pageNo   当前页
     * @param pageSize 当前页数
     * @param params   检索参数
     */
    @Override
    public PageInfo<IProjectInfo> findPage(Integer pageNo, Integer pageSize, Map<String, Object> params) {
        if (!StringUtil.checkObj(pageNo)) {
            logger.warn("method[findPage] pageNo is null. pageNo = 1");
            pageNo = 1;
        }
        if (!StringUtil.checkObj(pageSize)) {
            logger.warn("method[findPage] pageSize is null. pageSize = 5");
            pageSize = 5;
        }
        PageHelper.startPage(pageNo, pageSize);
        List<IProjectInfo> dataList = projectInfoDao.findPage(params);
        PageInfo<IProjectInfo> pageInfo = new PageInfo<>(dataList);
        return pageInfo;
    }

    /**
     * 根据用户id获取项目列表数据
     *
     * @param sysUserId 用户id
     */
    @Override
    public List<IProjectInfo> findProjectListBySysUserId(Long sysUserId) {
        if (!StringUtil.checkObj(sysUserId)) {
            return null;
        }
        Map params = new HashMap();
        params.put("sysUserId", sysUserId);
        List<IProjectInfo> dataList = projectInfoDao.findProjectListBySysUserId(params);
        return dataList;
    }

    /**
     * 根据用户id分页获取项目数据
     *
     * @param pageNo      当前页
     * @param pageSize    当前页数
     * @param projectName 项目名称
     * @param sysUserId   用户id
     * @return 分页数据
     */
    @Override
    public PageInfo<IProjectInfo> findProjectInfoPage(Integer pageNo, Integer pageSize, String projectName, Long sysUserId) {
        if (!StringUtil.checkObj(pageNo)) {
            logger.warn("method[findProjectInfoPage] pageNo is null. pageNo = 1");
            pageNo = 1;
        }
        if (!StringUtil.checkObj(pageSize)) {
            logger.warn("method[findProjectInfoPage] pageSize is null. pageSize = 5");
            pageSize = 5;
        }
        Map params = new HashMap();
        params.put("projectName", projectName);
        params.put("sysUserId", sysUserId);
        PageHelper.startPage(pageNo, pageSize);
        List<IProjectInfo> dataList = projectInfoDao.findProjectListBySysUserId(params);
        PageInfo<IProjectInfo> pageInfo = new PageInfo<>(dataList);
        return pageInfo;
    }

}
 



